﻿using System;
using System.Collections.Generic;
using ControleRepresentante.Logic.Models;
using WebSite.ViewModels.Fields;

namespace WebSite.ViewModels.EditViewModels
{
    public class RelatorioVisitaEditViewModel : AttributeHolderEditViewModel
    {
        private readonly Dictionary<Guid, string> m_Clientes;
        private readonly Dictionary<Guid, string> m_Vendedores;

        public RelatorioVisitaEditViewModel(Dictionary<Guid, string> clientes, Dictionary<Guid, string> vendedores)
            : base("Relatório de Visita")
        {
            m_Clientes = clientes;
            m_Vendedores = vendedores;
        }

        protected override IEnumerable<IField> GetGeralFields()
        {
            RelatorioVisita relatorioVisita = (RelatorioVisita) Model;

            List<IField> result = new List<IField>
            {
                new DatePickerField("Data", "data", relatorioVisita.Data),
                CreateDropDownField("Cliente", "clienteId", m_Clientes, x => x.Cliente),
                CreateDropDownField("Vendedor", "vendedorId", m_Vendedores, x => x.Vendedor),
                new TextAreaField("Observação", "observacao", relatorioVisita.Observacao)
            };
            result.AddRange(base.GetGeralFields());
            return result;
        }

        private DropDownListField CreateDropDownField(string caption, string name, Dictionary<Guid, string> dictionary, Func<RelatorioVisita, ModelBase> getModelFromRelatorioVisita)
        {
            RelatorioVisita relatorioVisita = (RelatorioVisita) Model;

            DropDownListField downListField = new DropDownListField(caption, name, true);
            foreach (KeyValuePair<Guid, string> model in dictionary)
            {
                ModelBase modelFromPedido = getModelFromRelatorioVisita(relatorioVisita);
                downListField.Add(model.Value, model.Key, modelFromPedido != null && (modelFromPedido.Id == model.Key));
            }
            return downListField;
        }
    }
}